/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-09-14 19:47:12
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-09-19 16:20:28
 * @FilePath: \郭招娣\hou\app\controller\articleLists.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-09-12 10:40:26
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-09-16 22:12:08
 * @FilePath: \郭招娣\hou\app\controller\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
'use strict';

const Controller = require('egg').Controller;
class ArticleListController extends Controller {
    // 获取文章列表

    async accessLists() {
        const { ctx } = this;
        const { pageCode, pageSize, url, ip,ie,kernel,option,address,num,date } = ctx.request.body;
        // console.log(user,'11')
        // console.log(status, 'kkkka')

        let lists = await this.app.mysql.select('accessLists');
        console.log(lists.length)

        let newLists = lists;
        // console.log(num, 'ii')
        if (url) {
            newLists = newLists.filter(v => v.url.includes(url))
        }
        if (ip) {
            newLists = newLists.filter(v => v.ip.includes(ip))
        }
        if (ie) {
            newLists = newLists.filter(v => v.ie.includes(ie))
        }
        if (kernel) {
            newLists = newLists.filter(v => v.kernel.includes(kernel))
        }
        if (option) {
            newLists = newLists.filter(v => v.option.includes(option))
        }
        if (num) {
            newLists = newLists.filter(v => v.num===Number(num))
        }
        if (address) {
            newLists = newLists.filter(v => v.address.includes(address))
        }
        if (date) {
            newLists = newLists.filter(v => v.num.includes(date))
        }
        let total = newLists.length;
        newLists = newLists.slice((pageCode - 1) * pageSize, pageCode * pageSize)
        if (newLists.length) {
            ctx.body = {
                code: 200,
                total,
                results: newLists
            }
        } else {
            ctx.body = {
                code: 400,
                msg: '获取数据失败'
            }
        }
    }

    // 删除文章列表
    async deleteAccessLists() {
        const { ctx } = this;
        const { id } = ctx.query;
        const result = await this.app.mysql.delete('accessLists', { id });
        if (result) {
            const list = await this.app.mysql.select('accessLists');
            ctx.body = {
                code: 200,
                msg: '删除成功',
                list
            }
        } else {
            ctx.body = {
                code: 200,
                msg: '删除失败',
                list
            }
        }

    }




    // 多选删除
    async accessDelete() {
        const { ctx } = this;
        const { multipleSelection, pageCode, pageSize } = ctx.request.body;
        const chooseLists = multipleSelection.map(v => {
            return this.app.mysql.delete('searchLists', { id: v.id })
        });

        if (chooseLists) {
            const list = await this.app.mysql.select('accessLists');
            if (list) {
                ctx.body = {
                    code: 200,
                    msg: '删除成功',
                    list: list.slice((pageCode - 1) * pageSize, pageCode * pageSize),
                    total: list.length
                }
            } else {
                ctx.body = {
                    code: 404,
                    msg: '删除失败',
                }
            }
        } else {
            ctx.body = {
                code: 404,
                msg: '多选删除错误',
            }
        }
    }



}

module.exports = ArticleListController;
